<?php
/**
 * Author: LaiGuiming
 * Time: 2017-08-30 17:45:32
 * Copyright (C) 2016 Longyan Wehome Message CO.,LTD.
 */
namespace app\admin\controller;
use app\admin\model\Versions;
use app\common\controller\BackEnd;
use think\Cache;

/**
 * 首页控制器
 * Class Index
 * @package app\admin\controller
 */
class Index extends BackEnd
{
    /**
     * 首页
     * @return mixed
     */
    public function index()
    {
        $menus = getMenus();
        $this->assign('menus', $menus);
        return $this->fetch();
    }

    /**
     * 欢迎页
     * @return mixed
     */
    public function welcome()
    {
        //获取版本信息
        $vers = Versions::where("")->order("ct desc")->select();
        $this->assign('vers', $vers);
        $this->assign('vid', $vers[0]['id']);
        return $this->fetch();
    }

    /**
     * 登录页
     * @return mixed
     */
    public function login()
    {
        return $this->fetch("index/login".cache('login_type'));
    }

    /**
     * 清空系统缓存
     */
    public function cleanCache()
    {
        delFile(RUNTIME_PATH);
        Cache::clear();
        //清除session
        session(null);
        $this->success("清空缓存成功!", U('admin/index/login'));
    }

    /**
     * 登录操作
     */
    public function dologin()
    {
        //判断验证码
        if (cache("captcha_flag") == 1) {
            $captcha = $_POST['captcha'];
            if (!captcha_check($captcha)) {
                //验证失败
                $this->resError( "验证码不正确！");
                return;
            };
        }

        $data = D("user")->where("status=0  and login_name='" . $_POST['login_name'] . "' and pwd='" . md5($_POST['pwd']) . "'")->find();
        if ($data) {
            session('user', $data);
            session('head_img', $data['head_img']);
            $this->saveLog("登录系统");

            //加载数据字典到redis
            $dict_flag = cache('dict_flag');
            if ($dict_flag == false) {
                cache('dict_flag', 1);
                $dicts = D("dict")->where("status=0")->select();
                foreach ($dicts as $temp) {
                    cache($temp['code'], $temp['val']);
                }
            }

            $this->resSuccess();
        } else {
            $this->resError( "用户名或密码不正确！");
        }

    }

    /**
     * 退出登录
     */
    public function logout()
    {
        //清除session
        session(null);
        //页面跳转
        return redirect('admin/index/login');
    }
}
